package cn.wen.yaling.xo.service.impl;

import cn.wen.yaling.commons.constant.Constants;
import cn.wen.yaling.commons.constant.RedisConstants;
import cn.wen.yaling.commons.enums.LogStatus;
import cn.wen.yaling.commons.service.RedisService;
import cn.wen.yaling.commons.utils.*;
import cn.wen.yaling.commons.vo.ArticleListVO;
import cn.wen.yaling.commons.vo.ExceptionLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.wen.yaling.xo.dao.ExceptionLogDao;
import cn.wen.yaling.commons.entity.ExceptionLogEntity;
import cn.wen.yaling.xo.service.ExceptionLogService;


@Service("exceptionLogService")
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogDao, ExceptionLogEntity> implements ExceptionLogService {

    @Autowired
    private ExceptionLogDao exceptionLogDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ExceptionLogEntity> page = this.page(
                new Query<ExceptionLogEntity>().getPage(params),
                new QueryWrapper<ExceptionLogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public ResultEntity getExceptionLogList() {
        List<ExceptionLogVO> exceptionList;
        // 1、先判断redis缓存中是否能命中缓存
        if (redisService.hasKey(RedisConstants.CACHE_ADMIN_EXCEPTION_LOG_LIST)) {
            exceptionList = redisUtils.getListCache(RedisConstants.CACHE_ADMIN_EXCEPTION_LOG_LIST, ExceptionLogVO.class);
        } else {
            // 2、缓存不命中则到db中获取
            QueryWrapper<ExceptionLogEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("exception_status", LogStatus.NORMAL_USE.getStatus());
            List<ExceptionLogEntity> exceptionLogEntities = exceptionLogDao.selectList(queryWrapper);
            if (exceptionLogEntities == null) return ResultEntity.error();
            exceptionList = exceptionLogEntities.stream().map((item) -> {
                ExceptionLogVO target = new ExceptionLogVO();
                BeanUtils.copyProperties(item, target);
                return target;
            }).collect(Collectors.toList());
            // 3、将db中的缓存获取到
            redisUtils.setListCache(RedisConstants.CACHE_ADMIN_EXCEPTION_LOG_LIST, exceptionList);
        }
        return ResultEntity.ok().put("data", exceptionList);
    }

    @Override
    public ResultEntity searchExceptionLogList(String startTime) {
        QueryWrapper<ExceptionLogEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(startTime)) {
            String[] time = startTime.split(",");
            if (time.length == Constants.NUM_TWO) {
                queryWrapper.between("create_time", DateUtils.str2Date(time[0]), DateUtils.str2Date(time[1]));
            }
        }
        queryWrapper.eq("exception_status", LogStatus.NORMAL_USE.getStatus());
        List<ExceptionLogEntity> exceptionLogEntities = exceptionLogDao.selectList(queryWrapper);
        if (exceptionLogEntities.size() == 0) return ResultEntity.error();
        List<ExceptionLogVO> collect = exceptionLogEntities.stream().map((item) -> {
            ExceptionLogVO target = new ExceptionLogVO();
            BeanUtils.copyProperties(item, target);
            return target;
        }).collect(Collectors.toList());
        return ResultEntity.ok().put("data", collect);
    }

    @Override
    public ResultEntity deleteBatchExceptionLogByIds(Integer[] exceptionLogIds) {
        boolean res = exceptionLogDao.updateStatusByIds(exceptionLogIds, LogStatus.DELETED.getStatus());
        if (res) {
            // 删除缓存
            redisService.del(RedisConstants.CACHE_ADMIN_EXCEPTION_LOG_LIST);
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

}